package com.itlong.cloud.controller.app;

import com.itlong.cloud.POJO.DTO.operate.OperateAppFunctionGetPageDTO;
import com.itlong.cloud.POJO.PO.MiniProgramFunctionInfoPO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.app.AppFunctionInfoVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.noncore.operate.IOperateMiniProgramFunctionServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.words.NumberHandlerUtil;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <desc>
 *      运营套餐管理-小程序功能管理控制器
 * </desc>
 *
 * @createDate2020/3/20
 **/
@RestController
@RequestMapping("/packageManage/miniProgram")
public class OperateMiniProgramFunctionController extends BaseController {
    private static Logger LOG = LoggerFactory.getLogger(OperateAppHomePageController.class);

    @Autowired
    private IOperateMiniProgramFunctionServiceClient iOperateMiniProgramFunctionServiceClient;

    /**
     * <desc>
     *      分页获取小程序功能列表
     * </desc>
     *
     * @param operateAppFunctionGetPageDTO 参数实体
     * @return 小程序功能分页列表
     * @Author Zhu.yj
     * @createDate  2020/3/23
     */
    @ResponseBody
    @AuthAccessRequired
    @RequestMapping(value = "/getFunctionPage",method = RequestMethod.POST)
    public Object getFunctionPage(OperateAppFunctionGetPageDTO operateAppFunctionGetPageDTO, HttpServletRequest request){
        try {
            operateAppFunctionGetPageDTO = HttpProtocolUtil.parseRequestParamToDTO(OperateAppFunctionGetPageDTO.class,request);
            Page<MiniProgramFunctionInfoPO> page = iOperateMiniProgramFunctionServiceClient.getFunctionPage(operateAppFunctionGetPageDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
        }catch (Exception e){
            throw new DataAccessException("【运营-套餐管理-分页获取小程序功能列表getFunctionPage】获取小程序功能列表失败",e);
        }
    }

    /**
     * <desc>
     *      更改小程序功能信息
     * </desc>
     *
     * @param miniProgramFunctionInfoPO 参数实体
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/23
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.OPERATE_UPDATE_MINI_PROGRAM_FUNCTION_LOG_DESC)
    @RequestMapping(value = "/updateFunction",method = RequestMethod.POST)
    public Object updateFunction(MiniProgramFunctionInfoPO miniProgramFunctionInfoPO,HttpServletRequest request){
        try {
            miniProgramFunctionInfoPO = HttpProtocolUtil.parseRequestParamToDTO(MiniProgramFunctionInfoPO.class,request);
            iOperateMiniProgramFunctionServiceClient.updateFunction(miniProgramFunctionInfoPO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营-套餐管理-更改小程序功能信息updateFunction】,更改小程序功能信息失败",e);
        }
    }

    /**
     * <desc>
     *      获取单个小程序功能
     * </desc>
     *
     * @param miniFunctionId 小程序功能
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/31
     */
    @ResponseBody
    @RequestMapping(value = "/getOne",method = RequestMethod.POST)
    public Object getOne(String miniFunctionId, HttpServletRequest request){
        try {
            miniFunctionId = request.getAttribute("miniFunctionId") == null ? null : request.getAttribute("miniFunctionId").toString();
            if (StringUtils.isBlank(miniFunctionId)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            MiniProgramFunctionInfoPO miniProgramFunctionInfoPO = iOperateMiniProgramFunctionServiceClient.getOne(miniFunctionId);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),miniProgramFunctionInfoPO);
        }catch (Exception e){
            throw new DataAccessException("【运营_套餐管理_获取单个小程序功能getOne】获取单个小程序信息失败",e);
        }
    }

    /**
     * <desc>
     *      批量删除小程序功能信息
     * </desc>
     *
     * @param miniFunctionIds 小程序功能id字符串，多个逗号隔开
     * @param request
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/23
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.OPERATE_DELETE_MINI_PROGRAM_FUNCTION_LOG_DESC)
    @RequestMapping(value = "/deleteFunction",method = RequestMethod.POST)
    public Object deleteFunction(String miniFunctionIds,HttpServletRequest request){
        try {
            miniFunctionIds = request.getAttribute("miniFunctionIds") == null ? null : request.getAttribute("miniFunctionIds").toString();
            if (StringUtils.isBlank(miniFunctionIds)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            iOperateMiniProgramFunctionServiceClient.deleteFunction(miniFunctionIds);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营-套餐管理-批量删除小程序功能信息deleteFunction】删除小程序功能失败",e);
        }
    }

    /**
     * <desc>
     *      启用或禁用小程序功能
     * </desc>
     *
     * @param miniFunctionId 小程序功能id
     * @param operType 操作类型 1启用 2禁用
     * @param request
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/23
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.OPERATE_BAN_OR_USE_MINI_FUNCTION_LOG_DESC)
    @RequestMapping(value = "/banOrUse",method = RequestMethod.POST)
    public Object banOrUse(String miniFunctionId,Integer operType, HttpServletRequest request){
        try {
            miniFunctionId = request.getAttribute("miniFunctionId") == null ? null : request.getAttribute("miniFunctionId").toString();
            operType = request.getAttribute("operType") == null ? null : Integer.parseInt(request.getAttribute("operType").toString());
            if (StringUtils.isBlank(miniFunctionId) || operType == null){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (!NumberHandlerUtil.isInRange(operType,1,2)){
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            iOperateMiniProgramFunctionServiceClient.banOrUse(miniFunctionId,operType);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营-套餐管理-启用或禁用小程序功能banOrUse】启用或禁用小程序功能失败",e);
        }
    }

    /**
     * <desc>
     *      获取子节点
     * </desc>
     *
     * @param miniFunctionId 小程序功能id
     * @param request
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/23
     */
    @ResponseBody
    @RequestMapping(value = "/getChilds",method = RequestMethod.POST)
    public Object getChilds(String miniFunctionId,HttpServletRequest request){
        try {
            miniFunctionId = request.getAttribute("miniFunctionId") == null ? null : request.getAttribute("miniFunctionId").toString();
            if (StringUtils.isBlank(miniFunctionId)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            List<AppFunctionInfoVO> appFunctionInfoVOList = iOperateMiniProgramFunctionServiceClient.getChilds(miniFunctionId);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),appFunctionInfoVOList);
        }catch (Exception e){
            throw new DataAccessException("【运营-套餐管理-获取子节点getChilds】获取子节点失败",e);
        }
    }

    /**
     * <desc>
     *      通过套餐id获取小程序功能
     * </desc>
     *
     * @param appPackageId 移动端套餐id
     * @param request
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/24
     */
    @ResponseBody
    @RequestMapping(value = "/getAllotFunction",method = RequestMethod.POST)
    public Object getAllotFunction(String appPackageId, HttpServletRequest request){
        try {
            appPackageId = request.getAttribute("appPackageId") == null ? null : request.getAttribute("appPackageId").toString();
            if (StringUtils.isBlank(appPackageId)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            List<AppFunctionInfoVO> appFunctionInfoVOS = iOperateMiniProgramFunctionServiceClient.getAllotFunction(appPackageId);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),appFunctionInfoVOS);
        }catch (Exception e){
            throw new DataAccessException("【运营_套餐管理_通过套餐获取小程序功能getAllotFunction】通过套餐id获取小程序功能失败",e);
        }
    }

    /**
     * <desc>
     *      添加小程序功能
     * </desc>
     *
     * @param miniProgramFunctionInfoPO 小程序功能实体
     * @param request
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/24
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.OPERATE_SAVE_MINI_PROGRAM_FUNCTION_LOG_DESC)
    @RequestMapping(value = "/saveFunction",method = RequestMethod.POST)
    public Object saveFunction(MiniProgramFunctionInfoPO miniProgramFunctionInfoPO,HttpServletRequest request){
        try {
            miniProgramFunctionInfoPO = HttpProtocolUtil.parseRequestParamToDTO(MiniProgramFunctionInfoPO.class,request);
            iOperateMiniProgramFunctionServiceClient.save(miniProgramFunctionInfoPO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_套餐管理_添加小程序功能addFunction】添加小程序功能失败",e);
        }
    }

}
